package cn.tedu._06artice.controller;

import cn.tedu._06artice.base.response.JsonResult;
import cn.tedu._06artice.base.response.StatusCode;
import cn.tedu._06artice.mapper.ArticleMapper;
import cn.tedu._06artice.mapper.UserMapper;
import cn.tedu._06artice.pojo.dto.UserArticleParam;
import cn.tedu._06artice.pojo.dto.UserLoginParam;
import cn.tedu._06artice.pojo.vo.ArticleVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpSession;
import org.hibernate.validator.constraints.Range;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/user/")
@ResponseBody
@Validated
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam:" + userLoginParam);
        userMapper.userLogin(userLoginParam);
        session.setAttribute("user",userLoginParam);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,userLoginParam);
    }
    @Operation(summary = "发表文章功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("artice")
    public JsonResult addArtice(@RequestBody @Validated UserArticleParam userArticleParam, HttpSession session) {
        log.debug("userArticleParam:" + userArticleParam);
//        UserLoginParam userLoginParam = (UserLoginParam) session.getAttribute("user");
//        if (userLoginParam==null){
//            throw new RuntimeException("请先登录");
//        }
        int i = articleMapper.insertArticle(userArticleParam);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
    @Operation(summary = "查询指定文章功能")
    @ApiOperationSupport(order = 30)
    @GetMapping("selectByArticleId/{id}")
    public JsonResult selectArticle(@PathVariable @Range(min = 1,max = 100) Long id ){
        List<ArticleVO> articleVO= articleMapper.selectArticle(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);


    }
}
